import {Message} from "element-ui";

export default function mockData(path) {
    return new Promise((resolve, reject) => {
        let filePath = path.replace(/\//g, '.').replace(/^\.|\.$/g, '');
        const index = filePath.indexOf('?');
        if (index > -1) {
            filePath = filePath.substring(0, index) + '.json';
        } else {
            filePath = filePath + '.json';
        }
        try {
            resolve(require(`./${filePath}`));
        } catch (e) {
            Message({message: `404 mock数据 “${filePath}” 不存在，请检查！`, type: 'error', duration: 5 * 1000})
            reject({code: 404, data: null, msg: `mock数据 “${filePath}” 不存在，请检查！`});
        }
    })
}
